package com.xyht.medicine_management_system.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.common.entity.MmsImportTask;
import com.xyht.medicine_management_system.modules.common.entity.MmsImportTaskDetail;
import com.xyht.medicine_management_system.modules.common.mapper.MmsImportTaskDetailMapper;
import com.xyht.medicine_management_system.modules.common.mapper.MmsImportTaskMapper;
import com.xyht.medicine_management_system.modules.common.service.MmsImportTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.user.entity.MmsUser;
import com.xyht.medicine_management_system.modules.user.mapper.MmsUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 批量导入任务表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-11-30
 */
@Service
public class MmsImportTaskServiceImpl extends ServiceImpl<MmsImportTaskMapper, MmsImportTask> implements MmsImportTaskService {

    @Resource
    private MmsImportTaskMapper mmsImportTaskMapper;
    @Resource
    private MmsImportTaskDetailMapper mmsImportTaskDetailMapper;
    @Resource
    private MmsUserMapper mmsUserMapper;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public ResponseResult getImportTaskList(Integer task_type, HttpServletRequest request, Integer pageSize, Integer pageNum) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        if (isNullOrEmpty(user_id)){
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        MmsUser mmsUser = mmsUserMapper.selectById(user_id);
        if (mmsUser == null){
            return CommonResult.failed(CommonCodeEnum.ACCOUNT_ERR);
        }
        QueryWrapper<MmsImportTask> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(task_type)){
            queryWrapper.lambda().eq(MmsImportTask::getTaskType,task_type);
        }
        queryWrapper.lambda().eq(MmsImportTask::getUserId,user_id).orderByDesc(MmsImportTask::getCreateTime);
        Page<MmsImportTask> page = new Page<>(pageNum,pageSize);
        mmsImportTaskMapper.selectPage(page,queryWrapper);

        List<MmsImportTask> list = page.getRecords();
        int total = (int)page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getImportTaskDetail(String task_id) {
        if (isNullOrEmpty(task_id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsImportTask smsImportTask = mmsImportTaskMapper.selectById(task_id);
        if (smsImportTask == null ){
            return CommonResult.failed(CommonCodeEnum.TASK_NOT_EXIST);
        }

        QueryWrapper<MmsImportTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MmsImportTaskDetail::getTaskId,task_id);
        List<MmsImportTaskDetail> list = mmsImportTaskDetailMapper.selectList(queryWrapper);
        return CommonResult.success(list);
    }
}
